home *** CD-ROM | disk | FTP | other *** search
- Path: druid.borland.com!usenet
- From: pete@borland.com (Pete Becker)
- Newsgroups: comp.lang.c++
- Subject: Re: Help! Newbie question ...
- Date: 21 Mar 1996 16:35:30 GMT
- Organization: Borland International
- Message-ID: <4is0gi$in8@druid.borland.com>
- References: <4ilkfu$41q@Kaos.deepcove.com>
- NNTP-Posting-Host: pbecker.borland.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <4ilkfu$41q@Kaos.deepcove.com>, saffleck@deepcove.com says...
- >
- >Is this legal ?
- >
- >class A {...};
- >
- >A f();
- >
- >main() {
- > A a = f();
- >}
- >
- >A f()
- >{
- > A fa;
- > return fa;
- >}
- >
- > Can you create an object on the stack within a function and then
- return
- >the object ? I know that objects created on the stack are deleted at block or
- > function exit, so is it ok to return them and use the value to initialize
- >another object?
- > I realize that there are ways to get around this such as dynamically
- >allocating the object and returning a pointer or passing the function an
- >object which the function can initialize in whatever way it wants. However,
- >I'd like to know if this can be done 'cus it seems more tidy since the
- >returned object is automaticly deleted on block exit and can therefore be
- >ignored if not needed. I'd also like to know for my own personal interest.
-
- Yes, it's legal. It works the same way as returning a built-in type: the
- compiler is responsible for generating code that handles copying the object to
- its destination. You are responsible for providing an assignment operator and
- copy constructor that work correctly if the compiler-generated ones won't do
- what you need.
-
-